package com.tianji.learning.mq;

import com.tianji.learning.mq.message.SignMessage;
import com.tianji.learning.service.IPointsRecordService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import static com.tianji.common.constants.MqConstants.Exchange.LEARNING_EXCHANGE;
import static com.tianji.common.constants.MqConstants.Key.SIGN_IN;
import static com.tianji.common.constants.MqConstants.Key.WRITE_REPLY;
import static com.tianji.common.constants.MqConstants.Queue.QA_POINTS_QUEUE;
import static com.tianji.common.constants.MqConstants.Queue.SIGN_POINTS_QUEUE;
import static com.tianji.learning.domain.enums.PointsRecordType.*;

/**
 * @author Administrator
 * @version 1.0
 * @description: TODO
 * @date 2024/11/17 14:54
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class LearningPointsRecordListener {
    private final IPointsRecordService pointsRecordService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = QA_POINTS_QUEUE,durable = "true"),
            exchange = @Exchange(name = LEARNING_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = WRITE_REPLY
    ))
    public void listeneSignMessage(Long userId){
        log.info("监听到互动问答积分消息，userId:{}",userId);
        pointsRecordService.addPointsRecord(userId,5,QA);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = SIGN_POINTS_QUEUE,durable = "true"),
            exchange = @Exchange(name = LEARNING_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = SIGN_IN
    ))
    public void listeneSignMessage(SignMessage message){
        log.info("监听到签到积分消息，message:{}",message);
        pointsRecordService.addPointsRecord(message.getUserId(),message.getPoints(),SIGN);
    }
}
